package cn.jbolt.ec.wx;

import cn.jbolt.common.model.BecMember;
import cn.jbolt.common.model.BecRetailerStaff;
import cn.jbolt.ec.member.MemberService;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.ehcache.CacheKit;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;



public class CommonService {

    private static final WxMpService wxMpService = GenService.getWxMpService("jmd");
    private static String api_token = "";

    @Inject
    MemberService memberService;


    public String user_access_token(String openid) {

        String access_token = CacheKit.get("mall_wx_access_token", openid);
        if (access_token != null) return access_token;
        String refresh_token = CacheKit.get("mall_wx_refresh_token", openid);
        if (refresh_token != null) {
            try {
                WxMpOAuth2AccessToken wxMpOAuth2AccessToken =
                        wxMpService.oauth2refreshAccessToken(refresh_token);
                CacheKit.put("mall_wx_access_token",
                        openid, wxMpOAuth2AccessToken.getAccessToken());
                CacheKit.put("mall_wx_refresh_token",
                        openid,
                        wxMpOAuth2AccessToken.getRefreshToken());
                return wxMpOAuth2AccessToken.getAccessToken();

            } catch (Exception e) {
                System.out.println(e.getMessage());
                return null;

            }


        }
        return null;


    }

    public WxMpUser auth_by_code(String code) {
        System.out.println("code:" + code);

        try {
            WxMpOAuth2AccessToken wxMpOAuth2AccessToken =
                    wxMpService.oauth2getAccessToken(code);

            WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
            String openid = wxMpUser.getOpenId();

            CacheKit.remove("mall_wx_access_token",openid);
            CacheKit.remove("mall_wx_refresh_token",openid);

            CacheKit.put("mall_wx_access_token",
                    openid, wxMpOAuth2AccessToken.getAccessToken());
            CacheKit.put("mall_wx_refresh_token", openid,
                    wxMpOAuth2AccessToken.getRefreshToken());


            Kv cond = Kv.by("openid_jmd",openid);
            BecMember becMember = memberService.findFirst(cond);
            if(becMember!=null){
                becMember.setNickname(wxMpUser.getNickname());
                becMember.update();
                BecRetailerStaff becRetailerStaff =
                        new BecRetailerStaff().dao().findByIdLoadColumns(becMember.getId(),"retailer_id");
                String s = "" +becMember.getId();
                if(becRetailerStaff!=null){
                    s+= ","+becRetailerStaff.getRetailerId();


                }
                wxMpUser.setSexDesc(s);

            }else{
                becMember = new BecMember();
                becMember.setBalance(0L).setSpreaderCredit(0)
                        .setIsDel(0).setNickname(wxMpUser.getNickname())
                        .setOpenidJmd(wxMpUser.getOpenId()).setFrom("site");
                if(!becMember.save()){
                    wxMpUser.setSexDesc("null");
                }else{



                    wxMpUser.setSexDesc(""+becMember.getId());

                }
            }

            return wxMpUser;


        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;

        }

    }
    //
    public WxMpUser user_info(String openid) {
        try{
            return wxMpService.getUserService().userInfo(openid);

        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;

        }




    }
}